package com.baizhi.Controller;

import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.annotation.DeleteCacheAnnotation;
import com.baizhi.entity.Article;
import com.baizhi.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @RequestMapping("insert")
    @DeleteCacheAnnotation
    public @ResponseBody Map insert(MultipartFile file, Article article, HttpServletRequest request) throws IOException {
        //获得绝对路径
        System.out.println(article+"上师========================");
        String realPath=request.getServletContext().getRealPath("/img");
        /*System.out.println(realPath+"===========绝对路径");*/
        //通过uuid获得新名字
        if(file!=null) {
            String newFileName = new Date().getTime() + "_" + file.getOriginalFilename();
            /* System.out.println(newFileName+"========文件名字");*/
            file.transferTo(new File(realPath + "/" + newFileName));
            article.setArticleImage("/img/" + newFileName);
        }
        Map map=articleService.insertArticle(article);
        return  map;
    }
    @RequestMapping("delete")
    @DeleteCacheAnnotation
    public @ResponseBody Map delete(int articleId){
        Map map=articleService.deleteArticleById(articleId);
        return map;
    }
    @RequestMapping("update")
    @DeleteCacheAnnotation
    public @ResponseBody Map update(Article article){
        Map map=articleService.updateArticle(article);
        return  map;
    }
    @RequestMapping("showAll")
    @AddCacheAnnotation
    public @ResponseBody Map showAll(int page,int limit){
        Map map=articleService.showArticle(page,limit);
        return map;
    }
    @RequestMapping("selectOne")
    @AddCacheAnnotation
    public @ResponseBody Article selectOne(int articleId){
        Article article=articleService.selectOne(articleId);
        return article;
    }


}
